#include <iostream>
#include <list>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>
using namespace std;
typedef vector<short> vectorshort;

short Majority_2(const vector<vectorshort>& in) {
	vector<vectorshort>::const_iterator i = in.begin();
 	short candidate = i->front();
    short count = 1;
	i++;
    while(i != in.end()) {
        if(i->front() == candidate) {
			count++;
		} else {
			count--;
		}
		if(count == 0){
			candidate = i->front();
			count = 1;
		}
		i++;
	}
	return candidate;
}

short Majority(const vector<vectorshort>& in, map<short, vectorshort>& out, vector<short>& eliminate) {
	short b = Majority_2(in);
	//cout << "Majority2:" << b << endl;
	short count = 0;
	short index = 0;
	vector<vectorshort>::const_iterator i = in.begin();
	map<short,vectorshort>::iterator it;
	while(i != in.end()) {
		// update hash
		it = out.find(i->front());
		if(it != out.end()) {
			it->second.push_back(index);
		}
		else {
			vector<short> value;
			value.push_back(index);
			out.insert(pair<short, vectorshort>(i->front(), value));
		}
		// check majority	
		//cout << "in ";
		//copy(i->begin(), i->end(), ostream_iterator<short>(cout, " "));
		//cout << endl;
		if(i->front() == b) count++;
		i++;
		index++;
	}
	//cout << "Count:" << count << endl;
	if(count > in.size()/2) return b;
	else { 
		// Get eliminations
		it = out.begin();
		eliminate.push_back(it->first);
		size_t size = it->second.size();
		while(++it != out.end()) {
			//cout << "index " << it->first << " indexes- ";
			//copy(it->second.begin(), it->second.end(), ostream_iterator<short>(cout, " "));
			//cout << endl;
			if(size > it->second.size()) {
				eliminate.clear();
				eliminate.push_back(it->first);
				size = it->second.size();
			} else if(size == it->second.size()) {
				eliminate.push_back(it->first);
			}
		}
		return -1;
	}
}
short Contest(vector<vectorshort>& ballots) {
	map<short, vectorshort> hash;
	vector<short> elimination;
	short nCandidate = ballots[0].size();
	short winner = -1;
	//cout << "Size:" << ballots.size() << endl;
	while(nCandidate > 1) {
		winner = Majority(ballots, hash, elimination);
		if(winner >= 0)
			break;
		else {
			vector<short>::iterator it = elimination.begin();
			while(it != elimination.end()) {
				vector<short>::iterator indexes = hash[(*it)].begin();
				while(indexes != hash[(*it)].end()) {
					//cout << "Before:";
					//copy(ballots[*indexes].begin(), ballots[*indexes].end(), ostream_iterator<short>(cout, " "));
					//cout << endl;
					rotate(ballots[*indexes].begin(), ballots[*indexes].begin()+1, ballots[*indexes].end());
					//cout << "After :";
					//copy(ballots[*indexes].begin(), ballots[*indexes].end(), ostream_iterator<short>(cout, " "));
					//cout << endl;
					indexes++;
				}
				it++;
			}
			hash.clear();
			elimination.clear();
		}
		nCandidate--;
	}
	return winner;
}

int main() {
	unsigned short nCases = 0;
	string blank_line;
	cin >> nCases;
	cin >> noskipws;
	getline(cin, blank_line);
	while(nCases > 0) {
		cin >> skipws; 
		unsigned short nCandidate = 0;
		vector<string> names;
		vector<vectorshort> ballots;
		cin >> nCandidate;
		cin >> noskipws;
		string name;
		getline(cin, name);
		while(nCandidate-- > 0) {
			getline(cin, name);
			names.push_back(name.substr(0, name.size()-1));
		}
		string bstr;
		stringstream tokens(stringstream::in | stringstream::out);
		tokens >> skipws; 
		getline(cin, bstr);
		// This is eating blankline having spaces
		while(bstr.size() > 1) {
			tokens << bstr;
			vector<short> choices;
			vector<short> uChoices;
			nCandidate = names.size();
			while(nCandidate-- > 0) {
				short choice = 0;
				tokens >> choice;
				if(choice == 0)
					goto NEXT_BALLOT;
				choices.push_back(choice-1);
			}
			unique_copy(choices.begin(), choices.end(), back_inserter(uChoices));
			if(choices.size() == names.size() && choices.size() == uChoices.size())
				ballots.push_back(choices);
NEXT_BALLOT:
			getline(cin, bstr);
		}
		short winner = Contest(ballots);
		if(winner >= 0)
			cout << names[winner] << endl;
		else
			copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
		nCases--;	
		if(nCases >= 1)
			cout << endl;
	}
	return 0;	
}
